package cn.mw.cmdb.entity;

import cn.mw.components.mongodb.entity.CriteriaOpsType;
import lombok.Data;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.ArrayList;
import java.util.List;

@Data
public class PropertyTypeRelation extends PropertyType<PropertyTypeRelation ,Object> {
    private String refTypeId;
    public static final String ID = "relation";
    private static List<String> finalList = new ArrayList<>();

    public PropertyTypeRelation() {
        setId(ID);
        setName("关系关联");
        setValueTip("");
        setSortNum(3);
    }

    @Override
    public String getValueTip() {
        PropertyType propertyType = getPropertyTypeManage().getPropertyType(refTypeId);
        return null == propertyType ? "" : propertyType.getValueTip();
    }

    @Override
    public boolean validateValue(Object value, PropertyTypeRef propertyTypeRef) throws Exception {
        PropertyType propertyType = getPropertyTypeManage().getPropertyType(refTypeId);
        return propertyType.validateValue(value, propertyTypeRef);
    }

    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        Criteria criteria = null;
        switch (opsType) {
            case exist:
                criteria = new Criteria(name).exists((boolean) value);
                break;
            case ne:
                criteria = new Criteria(name).ne(value);
        }
        return criteria;
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return null;
    }

    @Override
    public PropertyTypeRelation newObj() {
        return new PropertyTypeRelation();
    }

    @Override
    public void refresh() {
        finalList.clear();
    }


}
